package com.shujia.day08;

import java.util.ArrayList;
import java.util.Collection;

public class Demo03StudentCollection {
    public static void main(String[] args) {
        Collection arrayList = new ArrayList();
        arrayList.add(new Student("陆玉龙", 18, "bigdata30"));
        arrayList.add(new Student("汪雨", 19, "bigdata30"));
        arrayList.add(new Teacher("杨德广", 28, "bigdata30"));

        for (Object o : arrayList) {
            // com.shujia.day08.Student cannot be cast to com.shujia.day08.Teacher
            if (o instanceof Teacher) {
                Teacher o1 = (Teacher) o;
                System.out.println(o1);
                o1.teach();
            } else if (o instanceof Student) {
                Student o1 = (Student) o;
                System.out.println(o1);
                o1.learn();
            }else {
                System.out.println(o);
            }
        }

        /*
            对于上述的调用过程，有时会出现类型转换问题，那么如何解决？
                1.获取对象对其进行判断是否为某个类的对象或子类对象 如果是那么进行强制类型转换 再进行调用
                2.由于Java是强类型语言，对于一个集合一般情况下，都只存储一种类型的数据
                        为了对其进行限制，提供了泛型方式
         */
        // class ArrayList<E> => 其中<E> 表示泛型类型，给定的类型需要继承Object 引用类型
        //    E 表示当前集合中存储的所有元素的类型 为 E
        Collection<Student> students = new ArrayList<>();
        students.add(new Student("汪雨", 19, "bigdata30"));
        students.add(new Student("许康杰", 29, "bigdata30"));
        System.out.println(students);
        // 对于取到的每个元素的类型就可以确定
        for (Student student : students) {
            System.out.println(student);
            student.learn();
        }

//        Collection<int> ints = new ArrayList<>();
        Collection<Integer> ints = new ArrayList<>(); // 对于基本数据类型需要使用其包装类

        // 对于泛型E 满足多态 => 父类引用指向子类对象
        Collection<Person> peoples = new ArrayList<>();
        peoples.add(new Student("汪雨", 19, "bigdata30"));
        System.out.println(peoples);
//        for (Person people : peoples) {
//
//        }



    }
}

class Teacher {
    String name;
    int age;
    String clazz;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public Teacher(String name, int age, String clazz) {
        this.name = name;
        this.age = age;
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", clazz='" + clazz + '\'' +
                '}';
    }

    public void teach() {
        System.out.println(name + "老师" + "正在教授大数据..");
    }
}

class Person{

}

class Student extends Person{
    String name;
    int age;
    String clazz;

    public Student(String name, int age, String clazz) {
        this.name = name;
        this.age = age;
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", clazz='" + clazz + '\'' +
                '}';
    }

    public void learn() {
        System.out.println(name+"同学正在学习Java");
    }
}
